################################################################################
# Judicial Ideology as Text -- Figure A3: Static Psis by Opinion
# Truscott & Romano
# Updated February 2025
################################################################################


################################################################################
# Load Source
################################################################################

source('R/truscott_romano_source.R') # Load Source


################################################################################
# Figure A3 -- Static Thetas by Opinion Authorship
################################################################################

{

  figure_A3 <- wordshoals_static$High$psi_doc %>%
    left_join(decisions %>% dplyr::select(docket, author_last, term, opinion_id, vote), by = 'opinion_id', relationship =  'many-to-many') %>%
    distinct(opinion_id, .keep_all = T)  %>%
    rename(decision_type = vote) %>%
    mutate(decision_type = case_when(
      .default = 'Concurrence',
      decision_type == 'Majority' ~ 'Majority',
      decision_type %in% c('Dissent', 'Jurisdictional Dissent') ~ 'Dissent'),
      decision_type = factor(decision_type, levels = c('Majority', 'Concurrence', 'Dissent')),
      author_last = ifelse(author_last == 'Connor', "O'Connor", author_last)) %>%
    filter(!author_last %in% c('Barrett', 'Rehnquist')) %>%
    ggplot(aes(x = psi)) +
    labs(x = '\nPsi',
         y = 'Density\n') +
    geom_density(aes(fill = decision_type), alpha = 2/3) +
    geom_vline(xintercept = 0, linetype = 2, colour = 'black', alpha = 1/3) +
    scale_x_continuous(breaks = seq(-2, 2, 2), lim = c(-3, 3)) +
    facet_wrap(~author_last, scale = 'free_y') +
    scale_fill_manual(values = c('deepskyblue3', 'cornsilk', 'coral3')) +
    theme_minimal() +
    theme(panel.border = element_rect(size = 1, colour = 'black', fill = NA),
          axis.text.x = element_text(size = 14, colour = 'black'),
          axis.text.y = element_blank(),
          axis.title = element_text(size = 16, colour = 'black'),
          strip.background = element_rect(size = 1, colour = 'black', fill = 'grey'),
          strip.text = element_text(size = 14, colour = 'black'),
          legend.position = 'bottom',
          legend.title = element_blank(),
          legend.background = element_rect(size = 1, colour = 'black', fill = NA),
          legend.text = element_text(size = 14, colour = 'black'))

  ggsave(figure_A3, file = 'figures/Figure_A3.png',
         width = 10, height = 8, units = 'in', bg = 'white')

  ggsave('figures_tiff/Figure_A3.tiff',
         plot = figure_A3,
         device = 'tiff',
         dpi = 600,
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')

} # Figure A3 Static Opinion-Authored Psis
